import 'dart:convert';

import 'package:test/test.dart';
import 'package:json_wrapper_example/json_wrapper.dart';

void main() {
  final uri = Uri.parse('https://github.com/test');
  final uri2 = Uri.parse('https://github.com/success');

  final helper = HttpHelper();
  helper.isMock = true;
  helper.mockDatas[uri] = json.encode({
    'msg': '出错了',
    'code': 101,
    'data': {},
  });
  helper.mockDatas[uri2] = json.encode({
    'msg': 'ok',
    'code': 200,
    'data': {
      'name': '小茗同学',
    },
  });

  test('json decode', () async {
    await helper.fetch(uri: uri, fac: TestData.fromMap).listen((event) {
      print('name = ${event.name}');
    }).asFuture();

    await helper.fetch(uri: uri2, fac: TestData.fromMap).listen((event) {
      print('name = ${event.name}');
    }).asFuture();
  });
}

class TestData {
  String name;

  static TestData fromMap(res) {
    final testData = TestData();
    testData.name = res['name'];
    return testData;
  }
}
